//
//  HPNews.m
//
//  Created by 啊  on 16/4/24
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "HPNews.h"


NSString *const kHPNewsImgUrl = @"imgUrl";
NSString *const kHPNewsContentUrl = @"contentUrl";
NSString *const kHPNewsSketch = @"sketch";
NSString *const kHPNewsId = @"id";
NSString *const kHPNewsTitle = @"title";
NSString *const kHPNewsFavouriteNum = @"favouriteNum";
NSString *const kHPNewsType = @"type";
NSString *const kHPNewsCommentNum = @"commentNum";


@interface HPNews ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation HPNews

@synthesize imgUrl = _imgUrl;
@synthesize contentUrl = _contentUrl;
@synthesize sketch = _sketch;
@synthesize newsIdentifier = _newsIdentifier;
@synthesize title = _title;
@synthesize favouriteNum = _favouriteNum;
@synthesize type = _type;
@synthesize commentNum = _commentNum;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.imgUrl = [self objectOrNilForKey:kHPNewsImgUrl fromDictionary:dict];
            self.contentUrl = [self objectOrNilForKey:kHPNewsContentUrl fromDictionary:dict];
            self.sketch = [self objectOrNilForKey:kHPNewsSketch fromDictionary:dict];
            self.newsIdentifier = [self objectOrNilForKey:kHPNewsId fromDictionary:dict];
            self.title = [self objectOrNilForKey:kHPNewsTitle fromDictionary:dict];
            self.favouriteNum = [[self objectOrNilForKey:kHPNewsFavouriteNum fromDictionary:dict] doubleValue];
            self.type = [self objectOrNilForKey:kHPNewsType fromDictionary:dict];
            self.commentNum = [[self objectOrNilForKey:kHPNewsCommentNum fromDictionary:dict] doubleValue];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.imgUrl forKey:kHPNewsImgUrl];
    [mutableDict setValue:self.contentUrl forKey:kHPNewsContentUrl];
    [mutableDict setValue:self.sketch forKey:kHPNewsSketch];
    [mutableDict setValue:self.newsIdentifier forKey:kHPNewsId];
    [mutableDict setValue:self.title forKey:kHPNewsTitle];
    [mutableDict setValue:[NSNumber numberWithDouble:self.favouriteNum] forKey:kHPNewsFavouriteNum];
    [mutableDict setValue:self.type forKey:kHPNewsType];
    [mutableDict setValue:[NSNumber numberWithDouble:self.commentNum] forKey:kHPNewsCommentNum];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.imgUrl = [aDecoder decodeObjectForKey:kHPNewsImgUrl];
    self.contentUrl = [aDecoder decodeObjectForKey:kHPNewsContentUrl];
    self.sketch = [aDecoder decodeObjectForKey:kHPNewsSketch];
    self.newsIdentifier = [aDecoder decodeObjectForKey:kHPNewsId];
    self.title = [aDecoder decodeObjectForKey:kHPNewsTitle];
    self.favouriteNum = [aDecoder decodeDoubleForKey:kHPNewsFavouriteNum];
    self.type = [aDecoder decodeObjectForKey:kHPNewsType];
    self.commentNum = [aDecoder decodeDoubleForKey:kHPNewsCommentNum];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_imgUrl forKey:kHPNewsImgUrl];
    [aCoder encodeObject:_contentUrl forKey:kHPNewsContentUrl];
    [aCoder encodeObject:_sketch forKey:kHPNewsSketch];
    [aCoder encodeObject:_newsIdentifier forKey:kHPNewsId];
    [aCoder encodeObject:_title forKey:kHPNewsTitle];
    [aCoder encodeDouble:_favouriteNum forKey:kHPNewsFavouriteNum];
    [aCoder encodeObject:_type forKey:kHPNewsType];
    [aCoder encodeDouble:_commentNum forKey:kHPNewsCommentNum];
}

- (id)copyWithZone:(NSZone *)zone
{
    HPNews *copy = [[HPNews alloc] init];
    
    if (copy) {

        copy.imgUrl = [self.imgUrl copyWithZone:zone];
        copy.contentUrl = [self.contentUrl copyWithZone:zone];
        copy.sketch = [self.sketch copyWithZone:zone];
        copy.newsIdentifier = [self.newsIdentifier copyWithZone:zone];
        copy.title = [self.title copyWithZone:zone];
        copy.favouriteNum = self.favouriteNum;
        copy.type = [self.type copyWithZone:zone];
        copy.commentNum = self.commentNum;
    }
    
    return copy;
}


@end
